package com.xmh.autocode.util;

import com.xmh.autocode.inject.Inject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameUtils {


    /**
     * 首字母大写
     *
     * @param string
     * @return
     */
    public static String titleCase(String string) {
        return string.substring(0, 1).toUpperCase() + string.substring(1, string.length());
    }

    /**
     * 表名转换
     *
     * @param name
     * @return
     */
    public static String nameTransformation(String name) {
        if (name.indexOf("T_") == 0 || name.indexOf("t_") == 0) {
            name = name.substring(2, name.length());
        }

        Pattern linePattern = Pattern.compile("_(\\w)");
        name = name.toLowerCase();
        Matcher matcher = linePattern.matcher(name);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        String string = sb.toString();

        return string;
    }

    /**
     * 字段名称转换
     *
     * @param type
     * @return
     */
    public static String typeTransformation(String type) {
        if (type.equals("INT") || type.equals("TINYINT") || type.equals("SMALLINT") || type.equals("INT UNSIGNED")) {
            return "Integer";
        } else if (type.equals("VARCHAR") || type.equals("TEXT") || type.equals("LONGTEXT") || type.equals("JSON")) {
            return "String";
        } else if (type.equals("BIGINT")) {
            return "Long";
        } else if (type.equals("BIT")) {
            return "Boolean";
        } else if (type.equals("FLOAT")) {
            return "Float";
        } else if (type.equals("DOUBLE")) {
            return "Double";
        } else if (type.equals("TIMESTAMP")) {
            return "Timestamp";
        } else if (type.equals("DECIMAL") || type.equals("DECIMAL UNSIGNED")) {
            return "BigDecimal";
        } else if (type.equals("DATETIME") || type.equals("DATE")) {
            return "Date";
        }
        return type;
    }


    /**
     * 根据文件相对路径获取包路径
     *
     * @param path
     * @return
     */
    public static String getPackagePath(String path) {
        String str = path.split("src")[1];
        String substring = str.substring(1, str.length() - 1);

        if (!substring.startsWith("com")) {
            String string = path.split("java")[1];
            substring = string.substring(1, string.length() - 1);
        }

        String packPach = substring.replace("/", ".");
        return packPach;
    }


    /**
     * 名字转换
     *
     * @param voName    1
     * @param className 2
     * @param code      3
     * @return 4
     */
    public static String extracted(String voName, String className, String code) {
        String nowStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        String replace = code.replace("__LowercaseTtableName__", voName)
                .replace("__UpperCaseTtableName__", className)
                .replace("____Date____", nowStr)
                .replace("___EPath___", getPackagePath(Inject.getVo_Path()));

        for (Map.Entry<String, String> entry : Inject.getConfigFiles().entrySet()) {
            replace = replace.replace("__" + entry.getKey() + "__", getPackagePath(entry.getValue()));
        }

        for (Map.Entry<String, String> entry : Inject.getLogicFiles().entrySet()) {
            replace = replace.replace("__" + entry.getKey() + "__", getPackagePath(entry.getValue()));
        }

        return replace;
    }


}
